var lastMessageBoxLayer = 10000;
function MessageBox(params) {
    if (typeof(params) == "undefined") {
        this.evType = "over";
        this.Overlap = false;
        this.focusShowPos = false;
        this.zIndex = 10000;
        return;
    } else if (typeof(params) == 'string') {
        this.evType = params;
    } else {
        this.evType = typeof(params.evType) == 'undefined' ? 'over': params.evType;
    }
    this.Overlap = typeof(params.Overlap) == 'undefined' ? false: params.Overlap;
    this.focusShowPos = typeof(params.focusShowPos) == 'undefined' ? false: params.focusShowPos;
    if (typeof(params.position) != 'undefined') {
        this.sp_position = {
            left: params.position.left,
            top: params.position.top
        };
    }
    if (typeof(params.zIndex) != 'undefined') {
        this.zIndex = params.zIndex;
    } else {
        this.zIndex = 10000;
    }
};
MessageBox.prototype = {
    timer: null,
    msgbox: null,
    bindobj: null,
    incomingTimer: null,
    position: {},
    show: function(obj, msg, timeout, icon, onclick) {
        if (this.Overlap == false && $(obj).attr('hasMessageBox') == 'yes') return;
        $(obj).attr('hasMessageBox', 'yes');
        if (typeof(timeout) == "undefined") timeout = 1000;
        if (typeof(icon) == "undefined") icon = "msg";
        if (timeout == "button") {
            onclick = icon;
            icon = timeout;
            timeout = 1000;
        }
        var vtimeout = timeout;
        if (timeout == 0) vtimeout = 50;
        var ricon = icon;
        if (icon == "button") ricon = "question";
        var self = this;
        self.leftTimer = function() {
            if (icon == 'button') return;
            clearTimeout(self.timer);
            self.timer = setTimeout(function() {
                clearTimeout(self.timer);
                self.close(self);
            },
            vtimeout);
        };
        self.incomingTimer = function() {
            clearTimeout(self.timer);
        }
        this.bindobj = obj;
        this.msgbox = $('<div class="m_layer"><div class="bg"><div class="content"><div class="mini"><b class="' + ricon + '"></b>' + msg + (icon == 'button' ? '<div class="btnbox"><a class="button ok">确认</a><a class="button cancel">取消</a></div>': '') + '</div></div></div></div>').prependTo("body");
        this.msgbox.css("z-index", self.zIndex);
        if (this.evType == 'over') {
            $(obj).bind('mouseout', self.leftTimer);
            $(obj).bind('mouseover', self.incomingTimer);
            this.msgbox.bind('mouseover',
            function() {
                clearTimeout(self.timer);
            });
            this.msgbox.bind('mouseout', self.leftTimer);
        } else {
            $(obj).bind('blur', self.leftTimer);
        }
        var effDirection = "down";
        if (this.focusShowPos != "down" && ($(obj).offset().top + $(obj).outerHeight() + this.msgbox.outerHeight() - $('body').scrollTop() > $(window).height() || this.focusShowPos == 'up')) {
            effDirection = "up";
            self.position.top = ($(obj).offset().top - this.msgbox.outerHeight());
        } else {
            self.position.top = $(obj).offset().top + $(obj).outerHeight();
        }
        self.position.left = $(obj).offset().left;
        if (typeof(self.sp_position) != "undefined") {
            self.position = self.sp_position;
        }
        this.msgbox.css("left", self.position.left + "px");
        this.msgbox.css("top", self.position.top + "px");
        this.msgbox.css("z-index", lastMessageBoxLayer++);
        this.msgbox.hide();
        if (icon == "button") {
            this.msgbox.find(".ok").click(function() {
                if (typeof(onclick) == "undefined" || onclick(self) != false) {
                    self.close();
                }
            });
            this.msgbox.find(".cancel").click(function() {
                self.close();
            });
        }
        if (timeout != 0) self.leftTimer();
        if (typeof(jQuery.ui) != 'undefined' && effDirection == "up") {
            this.msgbox.show("slide", {
                direction: "up"
            },
            200);
        } else {
            this.msgbox.slideDown(200);
        }
        return this.msgbox;
    },
    close: function() {
        var self = this;
        $(self.bindobj).attr('hasMessageBox', '');
        this.msgbox.slideUp(200);
        setTimeout(function() {
            self.msgbox.remove();
            if (self.evType == 'over') {
                $(self.bindobj).unbind('mouseover', self.incomingTimer);
            }
            $(self.bindobj).unbind(self.evType == 'over' ? 'mouseout': 'blur', self.leftTimer);
        },
        200);
    }
};

function previewImage(file) {
    var MAXWIDTH = 128;
    var MAXHEIGHT = 128;
    var div = document.getElementById('preview');
    if (file.files && file.files[0]) {
        div.innerHTML = '<img id=imghead>';
        var img = document.getElementById('imghead');
        img.onload = function() {
            var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
            img.width = rect.width;
            img.height = rect.height;
            img.style.marginLeft = rect.left + 'px';
            img.style.marginTop = rect.top + 'px';
        }
        var reader = new FileReader();
        reader.onload = function(evt) {
            img.src = evt.target.result;
        }
        reader.readAsDataURL(file.files[0]);
    } else {
        var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
        file.select();
        var src = document.selection.createRange().text;
        div.innerHTML = '<img id=imghead>';
        var img = document.getElementById('imghead');
        img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
        var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
        status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
        div.innerHTML = "<div id=divhead style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;margin-left:" + rect.left + "px;" + sFilter + src + "\"'></div>";
    }
}
function clacImgZoomParam(maxWidth, maxHeight, width, height) {
    var param = {
        top: 0,
        left: 0,
        width: width,
        height: height
    };
    if (width > maxWidth || height > maxHeight) {
        rateWidth = width / maxWidth;
        rateHeight = height / maxHeight;
        if (rateWidth > rateHeight) {
            param.width = maxWidth;
            param.height = Math.round(height / rateWidth);
        } else {
            param.width = Math.round(width / rateHeight);
            param.height = maxHeight;
        }
    }
    param.left = Math.round((maxWidth - param.width) / 2);
    param.top = Math.round((maxHeight - param.height) / 2);
    return param;
}

$(function() {
	$("#bangumi_style_layer .styles a").click(function(){
		if($("#bangumi_style_layer .styles a.on").length == "6" && !$(this).hasClass("on")){
			(new MessageBox()).show(this, "最多只能选择6种风格", 1000, "warning");
		}else{
			if($(this).hasClass("on")){
				$(this).removeClass("on");
			}else{
				$(this).addClass("on");
				index = $(this).attr("styleid");
				//alert(index);
			}
			var arr1 = new Array();
			$("#bangumi_style_layer .styles a.on").each(function(){
				//$("#content").append($(this).attr("styleid"));
				 arr1 = arr1.concat($(this).attr("styleid")); 
			});
			$("#id_styleid").val(arr1);

		}
	})
	
	$("#btn_normal").click(function(){
		$("#btn_bangumi").removeClass("on");
		$("#btn_normal").addClass("on");
		$("#bangumi_person_layer").hide();
		$("#bangumi_area_layer").hide();
		$("#bangumi_version_layer").hide();
		$("#bangumi_style_layer").hide();
		$("#bangumi_date_layer").hide();
		$("#is_bangumi").val(0);
	})
})